using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System;

[System.Serializable]

public class AllColorEspecialAchieve : AchievementItem {
	
	private const string TAG = "All Colors Achievement: ";
	
	public List<Vector3> colors;
	private string forSave_HaveSave = "cubeColors_HaveSaved";
	private string forSave_SaveColorsKey = "cubeColors_SavedColors";
	
	public bool Teste()
	{
		if(this.colors.Count <= 0)
		{
			return true;
		}
		return false;
	}
	
	public void Setup()
	{
		int haveSave = PlayerPrefs.GetInt(forSave_HaveSave);
		if(haveSave == 1)
		{
			LoadAllColors();
			if(Teste())
			{
				SetCompleted();
			}
			
		}else
		{
			// usa a do editor mesmo
			Save();
		}
				
		//PlayerPrefs.DeleteAll();
	}
	
	public void TryRemoveColor(Vector3 color)
	{
		if(!this.completed){
		
		if(colors.Contains(color))
		{
			colors.Remove(color);
			Save();

			if(Teste())
			{
				SetCompleted();
			}
			
			Debug.Log(TAG + "Try Remove Color: Cor removida");
		}
		}
	}
	
	public void TryRemoveColor(Color color)
	{
		if(!completed){
		Vector3 tmpVector3 = ColorToVector3(color);
		if(colors.Contains(tmpVector3))
		{
			colors.Remove(tmpVector3);
			Save();
			
			if(Teste())
			{
				SetCompleted();
			}
			
		}
		
		}
		
	}
	public Vector3 ColorToVector3(Color color)
	{
		
		int red = 0;
		if(color.r > 0)
		{
			if(color.r < 1)
			{
				// color = 128	
				red = 128;
			}else
			{
				// color = 1;
				red = 255;
			}	
		}
		
		int green = 0;
		if(color.g > 0)
		{
			if(color.g < 1)
			{
				// color = 128	
				green = 128;
			}else
			{
				// color = 1;
				green = 255;
			}	
		}
		
		int blue = 0;
		if(color.b > 0)
		{
			if(color.b < 1)
			{
				// color = 128	
				blue = 128;
			}else
			{
				// color = 1;
				blue = 255;
			}	
		}
		
		Vector3 vectorFromColor = new Vector3(red, green, blue);
		return vectorFromColor;
		
	}
	
	public void Save()
	{
		StringBuilder sb = new StringBuilder();
		foreach (Vector3 item in colors) {
			sb.Append(item + "/");
		}
		string tmpColors = sb.ToString();
		
		PlayerPrefs.SetString(this.forSave_SaveColorsKey, tmpColors);
		
		PlayerPrefs.SetInt(this.forSave_HaveSave, 1);
	}
	
	public void LoadAllColors()
	{
		string tmpSavedColors = PlayerPrefs.GetString(forSave_SaveColorsKey);
		string tmpSeparetedColors = tmpSavedColors.Replace("(", "");
		tmpSavedColors = tmpSeparetedColors.Replace(")", "");
		
		string[] savedColors = tmpSavedColors.Split(new char[]{'/'});
		
		this.colors.Clear();

		string[] vetores ;
		foreach(string color in savedColors)
		{
			try{
			vetores = color.Split(new char[]{','});			
			Vector3 tmpVector3 = new Vector3(float.Parse(vetores[0]), float.Parse(vetores[1]), float.Parse(vetores[2]));
			colors.Add(tmpVector3);
			}catch(Exception e)
			{
				//Debug.Log(TAG + "Erro ao tentar transformar os dados lidos em Cores");
			}
		}		
	}
	
	public void SetCompleted()
	{
		this.completed = true;
		AchieveStorage.getInstance.getByName("Artist").SetCompleted();		
	}
	
	
}
